package com.shigu.wsysnatch.springrun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Properties;

/**
 * 类名：WsySnatch
 * 类路径：com.shigu.src.com.shigu.com.com.shigu.wsysnatch.springrun.WsySnatch
 * 创建者：王浩翔
 * 创建时间：2017-09-06 13:37
 * 项目：snatchwebpage
 * 描述：网页抓取
 */
@Component("wsySnatch")
@Scope("singleton")
public class WsySnatch {

    public static String generateFileLocation;

    @Value("${contextpath}")
    private String contextPath;

    @Value("${htmNameUrlPath}")
    private String htmNameUrlPath;

    private Properties snatchUrlsMap;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");

    @PostConstruct
    public void initUrlsMap() {
        snatchUrlsMap = new Properties();
        if (generateFileLocation != null) {
            contextPath = generateFileLocation;
        }
        String dir = System.getProperty("user.dir");
        try (BufferedInputStream bi = new BufferedInputStream(Thread.currentThread().getContextClassLoader().getResourceAsStream(htmNameUrlPath))) {
            snatchUrlsMap.load(bi);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(snatchUrlsMap);
    }

    public void snatch() {
        String timeStamp = sdf.format(new Date());
        String directName = contextPath + timeStamp.substring(0, 8);
        File parentDirect = new File(directName);
        if (!parentDirect.exists()) {
            parentDirect.mkdirs();
        }
        while (!parentDirect.exists()) {
        }
        snatchUrlsMap.entrySet().stream().forEach(o -> new Thread(new SingleUrlSnatcher((Map.Entry) o, directName, timeStamp)).start());
    }


    class SingleUrlSnatcher implements Runnable {
        String parentDirect;
        Map.Entry snatchNameUrl;
        String timeStamp;

        SingleUrlSnatcher(Map.Entry nameUrl, String parentDirect, String timeStamp) {
            this.snatchNameUrl = nameUrl;
            this.parentDirect = parentDirect;
            this.timeStamp = timeStamp;
        }

        public void run() {
            //抓取地址
            String url = snatchNameUrl.getValue().toString();
            //生成文件名
            String fileName = snatchNameUrl.getKey() + timeStamp + ".html";
            try {
                Document document = Jsoup.connect(url).get();
                if (snatchNameUrl.getKey().toString().startsWith("index")) {
                    generateIndexText(document,parentDirect,snatchNameUrl.getKey().toString()+"text",timeStamp);
                    document.body().append("    <script type=\"text/javascript\">\n" +
                            "layer.close(marketLay);\n" +
                            "    </script>");
                }
                if (snatchNameUrl.getKey().toString().startsWith("categoryman")) {
                    document.select("div#tjgoods").append(tjgoodsString("31", "22", null, RIGHT, parentDirect, timeStamp));
                    document.select("ul#tjgoods2").append(tjgoodsString("32", "5", null, BOTTOM, parentDirect, timeStamp));
                }
                if (snatchNameUrl.getKey().toString().startsWith("categorywoman")) {
                    document.select("div#tjgoods").append(tjgoodsString("31", "22", "16", RIGHT, parentDirect, timeStamp));
                    document.select("ul#tjgoods2").append(tjgoodsString("32", "5", "16", BOTTOM, parentDirect, timeStamp));
                }
                if (snatchNameUrl.getKey().toString().startsWith("iteminfotjgoodsman")) {
                    document.html(tjgoodsString("36", "15", "30", GOODSINFO_LEFT, parentDirect, timeStamp));
                }
                if (snatchNameUrl.getKey().toString().startsWith("iteminfotjgoodswoman")) {
                    document.html(tjgoodsString("36", "15", "16", GOODSINFO_LEFT, parentDirect, timeStamp));
                }
                File file = new File(parentDirect, fileName);
                if (!file.createNewFile()) {
                    return;
                }
                try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file)); BufferedReader bufferedReader = new BufferedReader(new StringReader(document.html()))) {
                    bufferedWriter.write("<base href=\"http://www.wsy.com\">\n");
                    try {
                        while (true) {
                            bufferedWriter.write(bufferedReader.readLine());
                            bufferedWriter.write("\n");
                        }
                    } catch (Exception eof) {
                        //读完一个文件
                        bufferedWriter.flush();
                    }
                } catch (IOException ignore) {
                }
                //Jsoup异常
            } catch (IOException ignore) {
            }
        }
    }

    static final int RIGHT = 0;
    static final int BOTTOM = 1;
    static final int GOODSINFO_LEFT = 2;


    public static void generateIndexText(Document document,String parentDirect,String type,String timestamp) {
        File file = new File(parentDirect, type + timestamp + ".txt");
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) {
            //bufferedWriter.write("推荐档口：");
            //bufferedWriter.newLine();
            for (Element element : document.select("div.tab-box").select("div").select("ul.tab-ul").select("li").select("a")) {
                //bufferedWriter.write(element.attr("href").substring(1));
                //bufferedWriter.write(":");
                bufferedWriter.write(element.select("p").text());
                bufferedWriter.write(element.select("span").text());
                bufferedWriter.newLine();
            }
            //bufferedWriter.newLine();bufferedWriter.newLine();bufferedWriter.newLine();
            bufferedWriter.flush();
            //bufferedWriter.write("热卖商品档口：");
            //bufferedWriter.newLine();
            for (Element li : document.select("ul.goods-ul").select("li").select("div.text.right").select("a")) {
                //bufferedWriter.append(li.attr("href").substring(1));
                //bufferedWriter.write(":");
                bufferedWriter.write(li.attr("title"));
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 市场的坑货要文本
     */
    private static void generateGoodsText(JSONArray goodsArray, String prefix, String type, String parentDirect, String timestamp) {
        File file = new File(parentDirect, type + timestamp + ".txt");
        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) {
            bufferedWriter.write(prefix);
            for (Object o : goodsArray) {
                JSONObject element = JSONObject.parseObject(JSON.toJSONString(o));
                bufferedWriter.newLine();
                bufferedWriter.write("档口:");
                bufferedWriter.write(element.getString("shopid"));
                bufferedWriter.write("商品：(");
                bufferedWriter.write(element.getString("mid"));
                bufferedWriter.write(") ");
                bufferedWriter.write(element.getString("title"));
            }
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String tjgoodsString(String type, String size, String pid, int location, String parentDirect, String timestamp) throws IOException {
        Connection connection = Jsoup.connect("http://www.wsy.com/boutique/manage/get-goods-list").data("type", type).data("size", size);
        if (!StringUtils.isEmpty(pid)) {
            connection.data("pid", pid);
        }
        Document document = connection.ignoreContentType(true).get();
        JSONObject resultObject = JSON.parseObject(document.text());
        JSONArray resultArray = resultObject.getJSONArray("data");
        String result = "";
        switch (location) {
            case RIGHT:
                generateGoodsText(resultArray,"商品库右侧：","categorymanright",parentDirect,timestamp);
                result = tjgoodsRightString(resultArray);
                break;
            case BOTTOM:
                generateGoodsText(resultArray,"商品库底部：","categorybottomright",parentDirect,timestamp);
                result = tjgoodsBottomString(resultArray);
                break;
            case GOODSINFO_LEFT:
                generateGoodsText(resultArray,"商品详情左侧：","goodsinfoleft",parentDirect,timestamp);
                result = tjgoodsGoodsInfoLeftString(resultArray);
                break;
        }
        return result;
    }

    /**
     * 商品库右侧商品
     *
     * @param resultArray
     * @return
     */
    public static String tjgoodsRightString(JSONArray resultArray) {
        StringBuffer result = new StringBuffer();
        resultArray.stream().forEach(o -> {
            JSONObject element = JSONObject.parseObject(JSON.toJSONString(o));
            result.append("<div class=\"tg-item\">\n")
                    .append("<div class=\"pic\"><a href=\"/item.htm?id=").append(element.get("mid")).append("\" target=\"_blank\">").append("<img src=\"").append(element.get("picurl")).append("\"></a>").append("</div>\n")
                    .append("<div class=\"pri\">")
                    .append("<span>￥</span><strong>").append(element.get("price")).append("</strong>")
                    .append("<div class=\"seller\">").append("<a href=\"/").append(element.get("uid")).append("\" target=\"_blank\">").append(element.get("shopid")).append("</a></div>")
                    .append("</div>\n")
                    .append("<div class=\"tit\" style=\"display: none;\"><div class=\"bgred\"></div>").append("<a href=\"/item.htm?id=").append(element.get("mid")).append("\" target=\"_blank\">").append(element.get("title")).append("</a></div>\n")
                    .append("</div>\n");
        });
        return result.toString();
    }

    /**
     * 商品库底部商品
     *
     * @param resultArray
     * @return
     */
    public static String tjgoodsBottomString(JSONArray resultArray) {
        StringBuffer result = new StringBuffer();
        resultArray.stream().forEach(o -> {
            JSONObject element = JSONObject.parseObject(JSON.toJSONString(o));
            result.append("<li><div class=\"b-hot-item\">\n")
                    .append("<div class=\"pic\"><a href=\"/item.htm?id=").append(element.get("mid")).append("\" target=\"_blank\"><img src=\"").append(element.get("picurl")).append("\"></a></div>\n")
                    .append("<div class=\"tit\"><a href=\"/item.htm?id=").append(element.get("mid")).append("\" target=\"_blank\">").append(element.get("title")).append("</a></div>\n")
                    .append("<div class=\"pri\"><span>¥</span><strong>").append(element.get("price")).append("</strong></div>\n")
                    .append("<div class=\"seller\"><a href=\"/").append(element.get("uid")).append("\" target=\"_blank\">").append(element.get("shopid")).append("</a></div>\n")
                    .append("</div></li>\n");

        });
        return result.toString();
    }

    /**
     * 生成商品详情页左侧推荐商品html
     *
     * @param resultArray
     * @return
     */
    public static String tjgoodsGoodsInfoLeftString(JSONArray resultArray) {
        StringBuffer result = new StringBuffer();
        result.append(WsyPageConstant.ITEM_INFO_PREFIX);
        resultArray.stream().forEach(o -> {
            JSONObject element = JSONObject.parseObject(JSON.toJSONString(o));
            result.append("<li class=\"ad_goods_li\"><div class=\"ad_goods_div\"><ul>\n")
                    .append("<li class=\"pic\"><a href=\"/item.htm?id=").append(element.get("mid")).append("\" target=\"_blank\"><img src=\"").append(element.get("picurl")).append("\"><span class=\"t\"></span></a></li>\n")
                    .append("<li class=\"pri fs-pri\"><span class=\"dk\"><a href=\"/").append(element.get("uid")).append("\" target=\"_blank\">").append(element.get("shopid")).append("</a></span>").append("<span>¥</span><strong>").append(element.get("price")).append("</strong></li>\n")
                    .append("<li class=\"tit\"><a href=\"/item.htm?id=").append(element.get("mid")).append("\" target=\"_blank\">").append(element.get("title")).append("</a></li>\n")
                    .append("</ul></div></li>\n");
        });
        result.append(WsyPageConstant.ITEM_INFO_AFTERFIX);
        return result.toString();
    }
}
